package com.lihan.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class PageConvertUtils {

    /**
     * 分页对象转换
     * @param source 原分页对象
     * @param converter 单个对象转换函数
     */
    public static <T, R> Page<R> convert(Page<T> source, Function<T, R> converter) {
        Page<R> target = new Page<>();
        // 复制分页信息
        target.setCurrent(source.getCurrent());
        target.setSize(source.getSize());
        target.setTotal(source.getTotal());
        target.setPages(source.getPages());
        
        // 转换records
        List<R> records = source.getRecords().stream()
                .map(converter)
                .collect(Collectors.toList());
        target.setRecords(records);
        
        return target;
    }
}